<template>
	<view>
		<view class="top-bg"></view>
		<view class="content-wrap">
			<view class="form-wrap">
				<view class="form-item yf-flex-left">
					<view class="form-label">
						姓名：
					</view>
					<input type="text" v-model="formData.userName"  maxlength="8" placeholder="请输入姓名" class="uni-input" />
				</view>
				<view class="form-item yf-flex-left">
					<view class="form-label">
						性别：
					</view>
					<picker @change="bindPickerChangeSex" range-key="text" :value="sexindex" :range="sexArr"
						class="uni-input">
						{{sexArr[sexindex].text||"请选择性别"}}
					</picker>
					<tui-icon name="arrowright" size="20" ></tui-icon>
				</view>
				<!-- <view class="form-item yf-flex-left">
					<view class="form-label">
						单位：
					</view>
					<input type="text" v-model="formData.company" placeholder="请输入单位" class="uni-input">
				</view> -->
				<view class="form-item yf-flex-left">
					<view class="form-label">
						部门：
					</view>
					<picker @change="bindPickerChange" range-key="name" :value="index" :range="deptList"
						class="uni-input">
						{{deptList[index].name||"请选择部门"}}
					</picker>
					<tui-icon name="arrowright" size="20" ></tui-icon>
				</view>
				<view class="form-item yf-flex-left">
					<view class="form-label">
						联系电话：
					</view>
					<input type="number" v-model="formData.phone" maxlength="11" placeholder="请输入联系电话"
						class="uni-input" disabled/>
				</view>
				<view class="form-item yf-flex-left">
					<view class="form-label">
						所在地：
					</view>
					<input type="text" v-model="formData.address" placeholder="请输入所在地" maxlength="30" class="uni-input"/>
				</view>
				<view class="form-item yf-flex-left">
					<view class="form-label">
						身份证号码：
					</view>
					<input type="idcard" v-model="formData.idCard" maxlength="18" placeholder="请输入身份证号码" class="uni-input"/>
				</view>
			</view>
			<view class="yf-flex-center submitBt" @click="formSubmit">编辑</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				formData: {
					address: "",
					avatar: "",
					company: "",
					deptId: "",
					deptName: "",
					gender: "",
					idCard: "",
					phone: "",
					userId: "",
					userName: "",
				},
				deptList: [], //部门列表
				index: "",
				sexArr: [{
					text: "女"
				}, {
					text: "男"
				}], //性别
				sexindex: "", //性别下标
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		onLoad() {
			this.getDeptList(); //获取部门列表
			this.formData = {
				...this.formData,
				...this.userInfo
			}
			if (this.formData.gender) { //性别复显
				this.sexindex = this.formData.gender == "男" ? 1 : 0
			}
		},
		methods: {
			...mapMutations(['SET_USERINFO']),
			// 部门选择
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.index = e.detail.value
				this.formData.deptId = this.deptList[e.detail.value].id
				this.formData.deptName = this.deptList[e.detail.value].name
			},
			//性别选择
			bindPickerChangeSex: function(e) {
				this.sexindex = e.detail.value
				this.formData.gender = this.sexArr[e.detail.value].text
			},
			// 部门列表
			getDeptList() {
				this.$apis.deptList().then((res) => {
					console.log(res, '@@@@@@@@')
					if (res.code == 0) {
						this.deptList = res.data
						if (this.formData.deptId) { //部门id复现
							res.data.forEach((item, index) => {
								if (this.formData.deptId == item.id) {
									this.index = index;
								}
							})
						}

					}
				})
			},
			// 更新用户信息
			getUserInfo() {
				this.$apis.userInfo().then((res) => {
					console.log(res, '@@@@@@@@')
					if (res.code == 0) {
						this.SET_USERINFO(res.data);
						setTimeout(()=>{
							this.$Router.navigateBack();
						},1500) 
					}
				})
			},
			// 提交
			formSubmit: function() {
				this.formData.userName=this.formData.userName.replace(/[, ]/g,'');
				this.formData.phone=this.formData.phone.replace(/[, ]/g,'');
				this.formData.address=this.formData.address.replace(/[, ]/g,''); 
				this.formData.idCard=this.formData.idCard.replace(/[, ]/g,'');
				uni.showModal({
					title: '修改',
					content: '是否确认提交修改？',
					confirmColor: "#3291F8",
					success: (res) => {
						if (res.confirm) {
							this.$api.showLoading()
							//验证通过！
							this.$apis.updateProfile(this.formData).then((res) => {
								this.$api.hideLoading()
								console.log(res, '@@@@@@@@')
								if (res.code == 0) {
									this.$api.msg('修改成功~')
									this.getUserInfo(); //更新用户信息
								} else {
									this.$api.msg(res.msg)
								}
							}).catch(() => {
								setTimeout(()=>{
									this.$api.hideLoading()
								},1500)
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

		}
	}
</script>

<style lang="scss">
	page {
		background: #F4F8FB;
	}

	.top-bg {
		background: #3291F8;
		width: 100%;
		height: 200rpx;
	}

	.content-wrap {
		padding: 0 30rpx;
		margin-top: -168rpx;
		width: 100%;

		.form-wrap {
			border-radius: 20rpx;
			background: #fff;
			padding: 22rpx 18rpx 22rpx;
			box-shadow: 0px 2rpx 6rpx 0px rgba(180, 180, 180, 40);

			.form-item {
				border-bottom: 1rpx solid #c9c9c9;
				height: 107rpx;
				padding: 0 24rpx;

				.form-label {
					font-size: 12+24rpx;
					width: 220rpx;
				}

				.uni-input {
					flex: 1;
					font-size: 12+24rpx;
				}
			}

			.form-item:last-child {
				border: none
			}

		}

		.submitBt {
			margin-top: 26rpx;
			width: 100%;
			height: 80rpx;
			border-radius: 50rpx;
			color: #fff;
			background: #3291F8;
			font-size: 12+24rpx;
		}
	}
</style>